<?php
include_once('pictogram.class.php');
include_once('myPDO.class.php');
class PictogramDAO {
    
        // Création d'un pictogramme 
	public function createPictogram(/* Pictogram */ $pictogram, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("INSERT INTO Pictogramme (nom,  image,son, text, Adulte_identifiant, CategoriePicto_idCategoriePicto) VALUES (:nom, :texte, :image,:son,:ad, :cat);");
			$nom = utf8_decode($pictogram->getNom());
                        $cat = utf8_decode($pictogram->getCategorie());
			$texte = utf8_decode($pictogram->getTexte());
                        $image = utf8_decode($pictogram->getImage());
                        $son = utf8_decode($pictogram->getSon());
                        $ad = utf8_decode($pictogram->getAdulte());
			$stmt->bindParam(':nom', $nom);
			$stmt->bindParam(':texte',$texte);
                        $stmt->bindParam(':image',$image);
                        $stmt->bindParam(':son',$son);
                        $stmt->bindParam(':cat',$cat);
                        $stmt->bindParam(':ad',$ad);
			$stmt->execute();
                        
                        $a = $db->lastInsertId();
                        return $a;
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
        // Mise à jour d'un PICTOGRAMME
        public function updatePictogram (/*Pictogram*/ $pictogram, /* myPDO*/ $db) {
            try {
                $stmt = $db->prepare("UPDATE Pictogramme SET nom=:nom, image=:image,son=:son ,text=:texte  WHERE idPicto=:id ;");
                $nom = utf8_decode($pictogram->getNom());
		$texte = utf8_decode($pictogram->getTexte());
                $image = utf8_decode($pictogram->getImage());
                $id = utf8_decode($pictogram->getIdentifiant());
                $son = utf8_decode($pictogram->getSon());
                $stmt->bindParam(':nom', $nom);
                $stmt->bindParam(':texte',$texte);
                $stmt->bindParam(':image',$image);
                $stmt->bindParam(':son',$son);
                $stmt->bindParam(':id',$id);
		$stmt->execute();
                
            }catch (Exception $e){
                    echo 'Exception reçu : ', $e->getMessage(), "\n" ;
            }
        }
      
        // Recherche d'un pictogramme selon l'ID 
        public function findPictogram(/* Eleve */ $pictogram, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Pictogramme WHERE idPicto = :id ;");
		$id = $pictogram->getIdentifiant();
		$stmt->bindParam(':id', $id);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return null ;
		}
		else {
			
			$pictogram = new Pictogram($row->idPicto, utf8_encode($row->nom), $row->CategoriePicto_idCategoriePicto,utf8_encode($row->text),utf8_encode($row->image),utf8_encode($row->son), $row->Adulte_identifiant);
			return $pictogram ;
		}
	}
        
        // Vérifier si un pictogramme appartient à une activité ou pas
        public function verifier($pictogram ,$db){
            $stmt = $db->prepare("SELECT * FROM Activite_has_Pictogramme where Pictogramme_idPicto = :id ;");
            $id = $pictogram->getIdentifiant();
            $stmt->bindParam (':id',$id);
            $res = $stmt->execute();
            $row = $stmt->fetchObject();
            if($row== null){
                return 0;
                
            }
            else {
                return 1;
            }
        }
        
          // Trouver les pictogrammes appartenant à des activités 
          public function find (/* Eleve */ $pictogram, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Activite_has_Pictogramme WHERE Pictogramme_idPicto = :id ;");
		$id = $pictogram->getIdentifiant();
		$stmt->bindParam(':id', $id);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return 0 ;
		}
		else {		
			return 1 ;
		}
	}
        
        // Trouver tous les Pictogrammes
	public function findAllPictogrammes(/* myPDO */ $db) {
		$tabRes = array() ;
		$i = 0 ;
		$stmt = $db->prepare("SELECT * FROM Pictogramme;");
		$res = $stmt->execute();
		while($row = $stmt->fetchObject()) {
			$pictogram = new Pictogram($row->idPicto, utf8_encode($row->nom), $row->CategoriePicto_idCategoriePicto,utf8_encode($row->text),utf8_encode($row->image),utf8_encode($row->son), $row->Adulte_identifiant);
			$tabRes[$i] = $pictogram ;
			$i++;
		}
		return $tabRes ;
	}
        
         // Rechercher les pictogrammes ayant comme nom ou texte commançant par le mot saisie 
         public function Rechercher($pictogram, $db) {
        $tabRes = array();
        $i = 0;
        $nom = $pictogram->getNom();
        $t = $nom."%";
        $stmt = $db->prepare("SELECT * FROM Pictogramme WHERE nom LIKE :nom OR text LIKE :nom ;");
        $stmt->bindParam(':nom', $t);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $picto = new Pictogram($row->idPicto, utf8_encode($row->nom), $row->CategoriePicto_idCategoriePicto,utf8_encode($row->text),utf8_encode($row->image),utf8_encode($row->son), $row->Adulte_identifiant);

            $tabRes[$i] = $picto;
            $i++;
        }
        return $tabRes;
    }
        
    // Trouver les pictogramme d'une catégorie   
    public function findPictogrammesByCategorie($id, /* myPDO */ $db) {
		$tabRes = array() ;
		$i = 0 ;
		$stmt = $db->prepare("SELECT * FROM Pictogramme WHERE CategoriePicto_idCategoriePicto = :id;");
		$stmt->bindParam(':id', $id);
		$res = $stmt->execute();
		while($row = $stmt->fetchObject()) {
			$pictogram = new Pictogram($row->idPicto, utf8_encode($row->nom), $row->CategoriePicto_idCategoriePicto,utf8_encode($row->text),utf8_encode($row->image),utf8_encode($row->son), $row->Adulte_identifiant);
			$tabRes[$i] = $pictogram ;
			$i++;
		}
		return $tabRes ;
	}
        
        
        // Trouver les pictogrammes crée par un adulte
        
         public function findPictogrammesByAdulte($id, /* myPDO */ $db) {
		$tabRes = array() ;
		$i = 0 ;
		$stmt = $db->prepare("SELECT * FROM Pictogramme WHERE Adulte_identifiant = :id;");
		$stmt->bindParam(':id', $id);
		$res = $stmt->execute();
		while($row = $stmt->fetchObject()) {
			$pictogram = new Pictogram($row->idPicto, utf8_encode($row->nom), $row->CategoriePicto_idCategoriePicto,utf8_encode($row->text),utf8_encode($row->image),utf8_encode($row->son), $row->Adulte_identifiant);
			$tabRes[$i] = $pictogram ;
			$i++;
		}
		return $tabRes ;
	}
        
        
     // Supprimer un pictogramme   
        public function deletePictogram (/*Pictogram*/ $pictogram, /*myPDO*/ $db) {
            try {
			$stmt = $db->prepare("DELETE FROM Pictogramme WHERE idPicto = :id ;");
			$id = $pictogram->getIdentifiant();
			$stmt->bindParam(':id', $id);
			$stmt->execute();
	
                
            }catch (Exception $e){
                echo 'Exception reçu : ', $e->getMessage (), "\n" ;
            }
        }
        
	
}

?>